package com.lgkj.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.lgkj.R;
import com.lgkj.bean.meitao.ComfirOrderBean;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/8/20 0020.
 */

public class OrderCarAdapter extends BaseAdapter implements View.OnClickListener {
    private static final String TAG = "OrderCarAdapter";
    private Context context;
    private List<ComfirOrderBean> carGoodsBean;
    private String level="0";
    public List<String> getCarId() {
        return carId;
    }

    private List<String> carId;

    private int type = 2;  //0  全选   1全不选    2自选

    public void setIsall(boolean isall) {
        this.isall = isall;
        this.type = isall ? 0 : 1;
        notifyDataSetChanged();
    }

    private boolean isall = true;

    public OrderCarAdapter(Context context) {
        this.context = context;
        carId = new ArrayList<>();
        carGoodsBean = new ArrayList<>();
        level= SharedPreferencesUtils.getParam(context,"level","-1").toString();
    }

    private CallBack callBack;

    public void setCallBack(CallBack back) {
        this.callBack = back;
    }


    public void setCarGoodsBean(List<ComfirOrderBean> dataBean) {
        this.carGoodsBean = dataBean;
    }

    @Override
    public int getCount() {
        return carGoodsBean.size();
    }

    @Override
    public ComfirOrderBean getItem(int i) {
        return carGoodsBean.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_car_order, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        ComfirOrderBean bean = carGoodsBean.get(i);
        if(level.equals("0")){
            viewHolder.tvPrice.setText(bean.getGoods_price1() + "");
        }else{
            viewHolder.tvPrice.setText(bean.getGoods_price() + "");
        }
        viewHolder.tvDesc.setText(bean.getGoods_name());
        viewHolder.tvNum.setText("x" + bean.getGoods_num() + "");
        Log.i(TAG, "getView: " + bean.toString());
        CommonUtils.LoadImage(context, viewHolder.ivImg, bean.getCover_id(), 0  );

//        bean.setTotalPrice(bean.getGoods_price() * bean.getGoods_num());
        return view;
    }

    @Override
    public void onClick(View view) {
        int position = Integer.parseInt(view.getTag().toString());
        switch (view.getId()) {
            case R.id.iv_jian:
                callBack.clickJian(position);
                break;
            case R.id.iv_add:
                callBack.clickAdd(position);
                break;
            case R.id.cb_car:
                CheckBox checkBox = (CheckBox) view;
                notifyDataSetChanged();
                break;
        }
    }


    public interface CallBack {
        void clickJian(int i);

        void clickAdd(int i);
    }

    static class ViewHolder {
        @BindView(R.id.iv_img)
        ImageView ivImg;
        @BindView(R.id.tv_desc)
        TextView tvDesc;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_num)
        TextView tvNum;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
